// 必填校验
export const required = {
  required: true,
  message: '请填写此项内容',
  trigger: 'blur'
}

// 手机号校验
export const phone = {
  pattern:
    /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/,
  message: '请输入正确的手机号',
  trigger: 'blur'
}

// 纯整数校验
export const integer = {
  pattern: /^(\-|\+)?\d+?$/,
  message: '请输入正确的数字',
  trigger: 'blur'
}

// 纯整数或者小数校验
export const float = {
  pattern: /^(\-|\+)?\d+(\.\d+)?$/,
  message: '请输入正确的数字（整数或小数）',
  trigger: 'blur'
}

// 日期校验
export const date = {
  type: 'date',
  message: '请选择日期',
  trigger: 'blur'
}

// 邮箱校验
export const email = {
  type: 'email',
  message: '请输入正确的邮箱地址',
  trigger: 'blur'
}

// 字符长度校验
export function len(max = 255, min = 0) {
  return {
    min,
    max,
    message: `长度在 ${min} 到 ${max} 个字符`,
    trigger: 'blur'
  }
}

// 数字范围校验
export function range(max = 99999999.99, min = 0) {
  const validator = (rule, val, cb) => {
    val = +val
    if (!(val >= min && val <= max)) {
      cb(new Error(rule.message))
    }
    cb()
  }
  return {
    validator,
    message: `数字范围在 ${min} 到 ${max}`,
    trigger: 'blur'
  }
}
